<?php
// $Id$

/**
 * Controller_Notice 控制器
 */
class Controller_Notification extends Controller_Abstract
{
    function actionIndex()
    {
        //历史通知显示
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();

        if($cur_user->permission_id != 2)
        {
            return $this->_redirect(url('default/index'));
        }

        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;
        $select = Notification::find('enterprise_id = ?', $cur_user->enterprise_id)->order('notification_id DESC')->all();

        $select->limitPage($page, 10);
        // 将分页信息和查询到的数据传递到视图
        $this->_view['pagination'] = $select->getPagination();
        $this->_view['Notificaiotns'] = $select->getAll();

        if($select->getCount() > 10)
        {
            $this->_view['num'] = 10;
        }
        else  
        {
            $this->_view['num'] = $select->getCount();
        }
        $this->_viewname = 'index';
    }

    function actiongetuserfromdepid()
    {
        $dep_id = $this->_context->dep_id;
        //echo "id:".$dep_id;
        //得到部门的用户

        $html="";
        $user_count=0;

        if($dep_id == -1)
        {
            if(null == $this->_app->currentUser())
            {
                return $this->_redirect(url('default/index'));
            }
            $cur_user = $this->_app->currentUserObject();
            if($cur_user->permission_id != 2)
            {
                return $this->_redirect(url('default/index'));
            }            
            $selects= User::find('enterprise_id = ?', $cur_user->enterprise_id)->all()->getALL();
            foreach($selects as $user)
            {
                $user_name = $user->username;
                if(strlen($user->user_realname) > 1)
                {
                    $user_name = $user->user_realname;
                }
                if(strlen($user->user_realname) < 1)
                {
                    continue;
                }

                /*$user_array[$user_count]={
                    "user_id" => $dep->user_id,
                    "user_name" => $user_name,
                };*/
                if($user_count == 0)
                {
                    $html = '<div class="nav"><ul>';
                }
                $html .= '<li><input type="checkbox" width="60px" name="user_select_id[]" checked="1" id="user_select_id" value="'.$user->id().'">'.$user_name.'</input></li>';
                $user_count++;
            }
        }
        else
        {
            $departmentusers = Userindepartment::find('department_id = ?', $dep_id)->all()->getAll();
            foreach($departmentusers as $dep)
            {
                $user = $dep->getUser();
                //echo 'id:'.$dep->user_id;
                if($user->id() != $dep->user_id)
                {
                    continue;
                }
                $user_name = $user->username;
                if(strlen($user->user_realname) > 1)
                {
                    $user_name = $user->user_realname;
                }
                if(strlen($user->user_realname) < 1)
                {
                    continue;
                }

                /*$user_array[$user_count]={
                    "user_id" => $dep->user_id,
                    "user_name" => $user_name,
                };*/
                if($user_count == 0)
                {
                    $html = '<div class="nav"><ul>';
                }
                $html .= '<li><input type="checkbox" width="60px" name="user_select_id[]" checked="1" id="user_select_id" value="'.$dep->user_id.'">'.$user_name.'</input></li>';
                $user_count++;
            }
        }


        if($user_count > 0)
        {
            $html .= '</ul></div>';
        }
        echo  $html;
        return;
    }

    function actionWrite()
    {
        //
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }

        $cur_user = $this->_app->currentUserObject();
        if(2 != $cur_user->permission_id)
        {
            return $this->_redirect(url('default/index'));
        }
        if(isset($_POST['notification_name']))
        {
            $not = new Notification();
            $not->notification_name =$_POST['notification_name'];
            $not->notification_desc =$_POST['notification_desc'];
            $not->enterprise_id = $cur_user->enterprise_id;
            $not->notification_create_time = intval(CURRENT_TIMESTAMP);
            $not->create_user_id = $cur_user->id();
            if(isset($_POST['department_id']))
            {
                $not->department_id = $_POST['department_id'];
            }
            if(isset($_POST['append_time']) && $_POST['append_time'] == 'on')
            {
                $time_string = date("Y-n-d, H:i");
                $not->notification_desc .= "\r\r\n\n\t\t---发布时间：$time_string";
            }
            $not->save();
            if(isset($_POST['user_select_id'])){
                $id=$_POST['user_select_id'];

                foreach ($id as $value){
                    $notifytouser = new NotifyToUser();
                    $notifytouser->notification_id = $not->id();
                    $notifytouser->user_id = $value;
                    $notifytouser->save();
                }
            }
            else
            {
                $departmentusers = 0;
                if($not->department_id == -1)
                {
                    $departmentusers = User::find('enterprise_id = ?', $cur_user->enterprise_id)->all()->getAll();

                }
                else
                {
                    $departmentusers = Userindepartment::find('department_id = ?', $not->department_id)->all()->getAll();
                }

                foreach($departmentusers as $user)
                {
                    $notifytouser = new NotifyToUser();
                    $notifytouser->notification_id = $not->id();
                    $notifytouser->user_id = $user->user_id;
                    $notifytouser->save();
                }
            }

            return $this->_redirectMessage(
                '提示您:',
                '通知发送成功',
                url('Notification/index'));
        }

        //找当前用户所在的企业的全部部门
        $deps = Department::find('enterprise_id = ?', $cur_user->enterprise_id)->all();

        $this->_view['deps'] = $deps->getAll();
        //$this->_view['depsCnt'] = $deps->getCount();

        $this->_viewname = 'write';
    }


    function actionDelete()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }

        if(isset($_POST['checkboxid'])){
            $id=$_POST['checkboxid'];

            foreach ($id as $value){
                //$department = department::find('department_id  = ?', $value)->query();
                //$department->department_hide_status=1;

                Notification::meta()->destroyWhere('notification_id = ?', $value);
                //删除栏目相关的文章
            }
        }

        return $this->_redirect(url('notification/index'));
    }

    function actionEdit()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }

        $cur_user = $this->_app->currentUserObject();

        $noti = Notification::find('notification_id = ?', $this->_context->notification_id)->query();

        if($noti->id() != $this->_context->notification_id)
        {
            return $this->_redirect(url('notification/index'));
        }

        if(isset($_POST['notification_name']))
        {
            $noti->notification_name =$_POST['notification_name'];
            $noti->notification_desc =$_POST['notification_desc'];
            //$noti->enterprise_id = $cur_user->enterprise_id;
            //$noti->notification_create_time = intval(CURRENT_TIMESTAMP);
            if(isset($_POST['department_id']))
            {
                $noti->department_id = $_POST['department_id'];
            }
            if(isset($_POST['append_time']) && $_POST['append_time'] == 'on')
            {
                $time_string = date("Y-n-d, H:i");
                $noti->notification_desc .= "\r\r\n\n\t\t---发布时间：$time_string";
            }
            $noti->save();
            if(isset($_POST['resend']))
            {
                if(isset($_POST['user_select_id'])){
                    $id=$_POST['user_select_id'];

                    foreach ($id as $value){
                        $notifytouser = new NotifyToUser();
                        $notifytouser->notification_id = $noti->id();
                        $notifytouser->user_id = $value;
                        $notifytouser->save();
                    }
                }
                else
                {
                    $departmentusers = 0;
                    if($noti->department_id == -1)
                    {
                        $departmentusers = User::find('enterprise_id = ?', $cur_user->enterprise_id)->all()->getAll();

                    }
                    else
                    {
                        $departmentusers = Userindepartment::find('department_id = ?', $noti->department_id)->all()->getAll();
                    }

                    foreach($departmentusers as $user)
                    {
                        $notifytouser = new NotifyToUser();
                        $notifytouser->notification_id = $noti->id();
                        $notifytouser->user_id = $user->user_id;
                        $notifytouser->save();
                    }
                }
            }

            return $this->_redirectMessage(
                '提示您:',
                '编辑成功',
                url('notification/index'));
        }

        //找当前用户所在的企业的全部部门
        $deps = Department::find('enterprise_id = ?', $cur_user->enterprise_id)->all();

        $this->_view['deps'] = $deps->getAll();
        $this->_view['noti'] = $noti;
        $this->_viewname = 'edit';
    }

    function actionPhoneReqNotificationList()
    {
        //返回通知ID列表
        //if(null == $this->_app->currentUser())
        if(!isset($this->_context->user_name) || empty($this->_context->user_name))
        {
            return 'failed';  
        }
        $cur_user = User::find('username = ?', $this->_context->user_name)->query();
        if($cur_user->username != $this->_context->user_name)
        {
            return 'failed';
        }
        $user_id = $this->_context->user_id;//增加一个user_id;
        $cur_user->last_check_time = intval(CURRENT_TIMESTAMP);// date();
        $cur_user->save();        

        //从notify_to_user表中查找当前用户没有查看的通知id
        $Noty_id_array = NotifyToUser::find("user_id = ? AND user_received_flag = 0", $cur_user->id())->all()->getAll();

        if(count($Noty_id_array) == 0)
        {
            return 'failed';
        }
        require 'genrss.php';

        $rss_gen=new RSS('电信', "http://www.baidu.com", '测试数据这里', '');
        foreach($Noty_id_array as $noti_id)
        {
            $Not = Notification::find('notification_id = ?', $noti_id->notification_id)->query();
            if($Not->id() == $noti_id->notification_id)
            {
                $noti_id->user_received_flag = 1;
                $noti_id->save();

                //检查是不是所有的用户都收到了通知,通知要通知到多少人，和已经通知到的人做比较
                $count_user_received = NotifyToUser::find("notification_id = ? AND user_received_flag = 1", $noti_id->notification_id)->all()->getCount();
                $count_user = 0;
                if($Not->department_id == -1)
                {
                    $count_user = User::find('enterprise_id = ?', $cur_user->enterprise_id)->all()->getCount();
                }
                else
                {
                    $count_user = Userindepartment::find('department_id = ?', $Not->department_id)->all()->getCount();
                }
                if($count_user == $count_user_received)
                {
                    NotifyToUser::meta()->destroyWhere('notification_id = ?', $noti_id->notification_id);
                }
                $rss_gen->AddItem($Not->notification_name, 'null', $Not->notification_desc, date('y-m-d H:i', $Not->notification_create_time ), 'null');
            }
        }

        return $rss_gen->Display();
    }

    //返回特定通知的内容
    function actionPhoneReqNewNotification()
    {
        $notification_id = $this->_context->notification_id;
        $noti = Notification::find('notification_id = ?', $this->_context->notification_id)->query();

        if($noti->id() != $this->_context->notification_id)
        {
            return 0;
        }

        $header_str='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8" /><body>';
        $html_header = $header_str.$noti->notification_name;
        $html_header .="\n";
        $html_tail = "\n</body>\n</html>";

        return $html_header.$noti->notification_desc.$html_tail;
    }

}


